<?php

namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;
require_once ('Common.php');
require_once ('Imessage.php');
require_once ('Dispatcher.php');

/**
 * 自动回复消息组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class ReplyMessage  extends Common   implements Imessage
{
    
	private $dispatcher;
    private  $touser;
    private $message;
    function __construct($appid,$secret,$token)
	{
        parent::__construct($appid,$secret,$token);
        $this->dispatcher = Dispatcher::getInstance();
	}
	function __destruct()
	{
        
	}
    /**
     * 发送者
     * @return type
     */
    public function getFrom()
    {
         return  $this->dispatcher->getReceiveTo();
    }
    /**
     * 接收者
     * @return type
     */
    public function setTouser($openid)
    {
         $this->touser = $openid;
         return $this;
    }
    /**
     * 接收者
     * @return type
     */
    public function getToUser()
    {
        if(empty($this->touser))
        {
            $this->touser = $this->dispatcher->getReceiveFrom();
        }
        return  $this->touser;
    }
	public function text($text = '')
	{
        $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'MsgType'      => self::MSGTYPE_TEXT,
            'Content'      => str_replace("\r\n", "\n", $text),
            'CreateTime'   => time(),
        );
       $this->Message($msg);
        return $this;
	}

	public function image($mediaid = '')
	{
          $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'MsgType'      => self::MSGTYPE_IMAGE,
            'Image'        => array('MediaId' => $mediaid),
            'CreateTime'   => time(),
        );
         $this->Message($msg);
        return $this;
	}
	public function voice($mediaid = '')
	{
         $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'MsgType'      => self::MSGTYPE_VOICE,
            'Voice'        => array('MediaId' => $mediaid),
            'CreateTime'   => time(),
        );
        $this->Message($msg);
        return $this;
	}
    /**
     * 
     * @param type $video
     * @return $this
     * array(
                'MediaId'     => $mediaid,
                'Title'       => $title,
                'Description' => $description
            )
     */
	 public function video($video) {
        $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'MsgType'      => self::MSGTYPE_VIDEO,
            'Video'        => $video,
            'CreateTime'   => time(),
        );
       $this->Message($msg);
        return $this;
    }
    public function music($title, $desc, $musicurl, $hgmusicurl = '', $thumbmediaid = '') {
        $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'CreateTime'   => time(),
            'MsgType'      => self::MSGTYPE_MUSIC,
            'Music'        => array(
                'Title'       => $title,
                'Description' => $desc,
                'MusicUrl'    => $musicurl,
                'HQMusicUrl'  => $hgmusicurl
            ),
        );
        if ($thumbmediaid) {
            $msg['Music']['ThumbMediaId'] = $thumbmediaid;
        }
       $this->Message($msg);
        return $this;
    }
	 public function news($newsData = array()) {
        $msg = array(
            'ToUserName'   => $this->getToUser(),
            'FromUserName' => $this->getFrom(),
            'CreateTime'   => time(),
            'MsgType'      => self::MSGTYPE_NEWS,
            'ArticleCount' => count($newsData),
            'Articles'     => $newsData,
        );
        $this->Message($msg);
        return $this;
    }
     /**
     * 设置发送消息
     * @param string|array $msg 消息数组
     * @param bool $append 是否在原消息数组追加
     * @return array
     */
    public function message($msg = '', $append = false) {
        if (is_null($msg)) 
        {
            $this->message = array();
        } 
        elseif (is_array($msg)) 
        {
            if ($append) 
            {
                $this->message = array_merge($this->message, $msg);
            } else 
            {
                $this->message = $msg;
            }
            return $this->message;
        }
        return $this->message;
    }
    /**
     * 回复微信服务器
     * @param type message 要发送的信息（默认取$this->message）
     * @param type $return 是否返回信息而不抛出到浏览器（默认:否）
     * @return boolean
     */
    public function send($send=true,$message=array())
    {
        if (empty($message)) {
            if (empty($this->message)) {   //防止不先设置回复内容，直接调用reply方法导致异常
                return false;
            }
            $message = $this->message;
        }
        $xmldata = Tools::arrToXml($message);
        if (!$send) 
        {
            return $xmldata;
        }
        echo $xmldata;
    }
}
?>